package com.yty.product.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FanoutAmqpConfig {
    @Autowired
    private ConnectionFactory connectionFactory;

    @Bean
    public AmqpTemplate fanoutAmqpTemplate() {
        RabbitTemplate template = new RabbitTemplate();
        template.setConnectionFactory(connectionFactory);
        template.setMessageConverter(new Jackson2JsonMessageConverter());
        template.setExchange("product.fanout.productInfoChanged");
        return template;
    }

    /**
     * 商品订单队列
     *
     * @return
     */
    @Bean
    public Queue fanoutProductOrder() {
        return new Queue("product.order");
    }

    /**
     * 商品短信队列
     *
     * @return
     */
    @Bean
    public Queue fanoutProductSms() {
        return new Queue("product.sms");
    }

    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("product.fanout.productInfoChanged");
    }

    @Bean
    public Binding bindingFanoutProductOrder(Queue fanoutProductOrder, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutProductOrder).to(fanoutExchange);
    }

    @Bean
    public Binding bindingFanoutProductSms(Queue fanoutProductSms, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutProductSms).to(fanoutExchange);
    }
}
